/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseOrganizeDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseOrganizeDao : BaseDao
    {
        public BaseOrganizeDao()
        {
            base.CurrentTableName = BaseOrganizeTable.TableName;
        }

        public BaseOrganizeDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseOrganizeDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseOrganizeEntity organizeEntity, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BaseOrganizeTable.FieldParentID, organizeEntity.ParentID, BaseOrganizeTable.FieldFullName, organizeEntity.FullName))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            if ((organizeEntity.Code.Length > 0) && this.Exists(BaseOrganizeTable.FieldCode, organizeEntity.Code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            str = this.AddEntity(organizeEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddByDetail(string parentID, string code, string fullName, string category, string outerPhone, string innerPhone, string fax, bool enabled, out string statusCode)
        {
            BaseOrganizeEntity organizeEntity = new BaseOrganizeEntity
            {
                ParentID = parentID,
                Code = code,
                FullName = fullName,
                Category = category,
                OuterPhone = outerPhone,
                InnerPhone = innerPhone,
                Fax = fax,
                Enabled = enabled
            };
            return this.Add(organizeEntity, out statusCode);
        }

        public string AddEntity(BaseOrganizeEntity organizeEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseOrganizeTable.TableName);
            organizeEntity.SortCode = sequence;
            sqlBuilder.BeginInsert(BaseOrganizeTable.TableName);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldID, sequence);
            this.SetEntity(sqlBuilder, organizeEntity);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseOrganizeTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseOrganizeEntity organizeEntity = new BaseOrganizeEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseOrganizeTable.FieldID, DataRowVersion.Original].ToString();
                    if (id.Length > 0)
                    {
                        num += this.DeleteEntity(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseOrganizeTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    organizeEntity.GetFrom(row);
                    num += this.UpdateEntity(organizeEntity);
                }
                if (row.RowState == DataRowState.Added)
                {
                    organizeEntity.GetFrom(row);
                    num += (this.AddEntity(organizeEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public void ClearProperty(BaseOrganizeEntity organizeEntity)
        {
            organizeEntity.ID = string.Empty;
            organizeEntity.Code = string.Empty;
            organizeEntity.ParentID = string.Empty;
            organizeEntity.FullName = string.Empty;
            organizeEntity.Category = string.Empty;
            organizeEntity.OuterPhone = string.Empty;
            organizeEntity.InnerPhone = string.Empty;
            organizeEntity.Fax = string.Empty;
            organizeEntity.Manager = string.Empty;
            organizeEntity.AssistantManager = string.Empty;
            organizeEntity.IsInnerOrganize = true;
            organizeEntity.Description = string.Empty;
            organizeEntity.Enabled = true;
            organizeEntity.SortCode = string.Empty;
            organizeEntity.CreateUserID = string.Empty;
            organizeEntity.CreateDate = string.Empty;
            organizeEntity.ModifyUserID = string.Empty;
            organizeEntity.ModifyDate = string.Empty;
        }

        public DataTable GetChildrenStaffs(string organizeID)
        {
            string[] organizeIDs = null;
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    organizeIDs = base.GetChildrensID(BaseOrganizeTable.FieldID, organizeID, BaseOrganizeTable.FieldParentID);
                    break;

                case DataBaseType.Sqlserver:
                case DataBaseType.Access:
                    {
                        string code = base.GetCode(organizeID);
                        organizeIDs = base.GetChildrensIDByCode(BaseOrganizeTable.FieldCode, code);
                        break;
                    }
            }
            BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
            return dao.GetDTByOrganizes(organizeIDs);
        }

        public DataTable GetCompanyList(string organizeID)
        {
            return DbLogic.GetDT(base.DbHelper, BaseOrganizeTable.TableName, BaseOrganizeTable.FieldCategory, "Company", BaseOrganizeTable.FieldSortCode);
        }

        public DataTable GetDepartmentList(string organizeID)
        {
            if (string.IsNullOrEmpty(organizeID))
            {
                return DbLogic.GetDT(base.DbHelper, BaseOrganizeTable.TableName, BaseOrganizeTable.FieldCategory, "Department", BaseOrganizeTable.FieldSortCode);
            }
            return DbLogic.GetDT(base.DbHelper, BaseOrganizeTable.TableName, BaseOrganizeTable.FieldParentID, organizeID, BaseOrganizeTable.FieldCategory, "Department", BaseOrganizeTable.FieldSortCode);
        }

        public BaseOrganizeEntity GetEntity(string id)
        {
            return new BaseOrganizeEntity(this.Get(id));
        }

        public DataTable GetInnerOrganize(string organizeID)
        {
            return this.GetDT(BaseOrganizeTable.FieldIsInnerOrganize, "1", BaseOrganizeTable.FieldSortCode);
        }

        public DataTable GetParentChildrenStaffs(string organizeID)
        {
            string[] organizeIDs = null;
            string code = base.GetCode(organizeID);
            organizeIDs = base.GetChildrensIDByCode(BaseOrganizeTable.FieldCode, code);
            BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
            return dao.GetDTByOrganizes(organizeIDs);
        }

        public int MoveTo(string id, string parentID)
        {
            return this.SetProperty(id, BaseOrganizeTable.FieldParentID, parentID);
        }

        public DataTable Search(string organizeID, string search)
        {
            search = BaseBusinessLogic.GetSearchString(search);
            string sqlQuery = " SELECT * FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.FieldCode + " LIKE '" + search + "'       OR " + BaseOrganizeTable.FieldFullName + " LIKE '" + search + "'       OR " + BaseOrganizeTable.FieldDescription + " LIKE '" + search + "' ORDER BY " + BaseOrganizeTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseOrganizeTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseOrganizeEntity organizeEntity)
        {
            sqlBuilder.SetValue(BaseOrganizeTable.FieldParentID, organizeEntity.ParentID);
            if (string.IsNullOrEmpty(organizeEntity.Code))
            {
                sqlBuilder.SetNull(BaseOrganizeTable.FieldCode);
            }
            else
            {
                sqlBuilder.SetValue(BaseOrganizeTable.FieldCode, organizeEntity.Code);
            }
            sqlBuilder.SetValue(BaseOrganizeTable.FieldFullName, organizeEntity.FullName);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldCategory, organizeEntity.Category);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldOuterPhone, organizeEntity.OuterPhone);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldInnerPhone, organizeEntity.InnerPhone);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldFax, organizeEntity.Fax);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldManager, organizeEntity.Manager);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldAssistantManager, organizeEntity.AssistantManager);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldIsInnerOrganize, organizeEntity.IsInnerOrganize ? 1 : 0);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldDescription, organizeEntity.Description);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldEnabled, organizeEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldSortCode, organizeEntity.SortCode);
        }

        public int Update(BaseOrganizeEntity organizeEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseOrganizeTable.TableName, organizeEntity.ID, organizeEntity.ModifyUserID, organizeEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BaseOrganizeTable.FieldParentID, organizeEntity.ParentID, BaseOrganizeTable.FieldFullName, organizeEntity.FullName, organizeEntity.ID))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return num;
            }
            if ((organizeEntity.Code.Length > 0) && this.Exists(BaseOrganizeTable.FieldCode, organizeEntity.Code, organizeEntity.ID))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            num = this.UpdateEntity(organizeEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseOrganizeEntity organizeEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseOrganizeTable.TableName);
            this.SetEntity(sqlBuilder, organizeEntity);
            sqlBuilder.SetValue(BaseOrganizeTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseOrganizeTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseOrganizeTable.FieldID, organizeEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}